import { isString, isEmpty } from "@pureadmin/utils";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import {
  useRouter,
  useRoute,
  type LocationQueryRaw,
  type RouteParamsRaw
} from "vue-router";

export function useDetail() {
  const route = useRoute();
  const router = useRouter();
  const getParameter = isEmpty(route.params) ? route.query : route.params;

  function toDetail(
    parameter: LocationQueryRaw | RouteParamsRaw,
    model:
      | "addSupInvoice"
      | "viewSupInvoice"
      | "editSupInvoice"
      | "invoSupInvoice"
      | "reInvoSupInvoice"
      | "exaimeSupInvoice"
      | "viewProcure"
      | "viewSupSettlement"
      | "viewSupplygoodsOrder"
      | "viewSupplyOrder"
  ) {
    Object.keys(parameter).forEach(param => {
      if (!isString(parameter[param])) {
        parameter[param] = parameter[param].toString();
      }
    });
    // 新建供应商发票
    if (model === "addSupInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_SupplierInvoice/components/supplierInvoiceDetail",
        name: "supplierInvoiceDetail",
        query: parameter,
        meta: {
          title: `【新建】供应商发票`
        }
      });
      router.push({ name: "supplierInvoiceDetail", query: parameter });
    }
    // 查看发票详情
    if (model === "viewSupInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/supplierInvoiceDetail",
        name: "supplierInvoiceDetail",
        query: parameter,
        meta: {
          title: `【查看】供应商发票详情`
        }
      });
      router.push({ name: "supplierInvoiceDetail", query: parameter });
    }
    // 编辑发票详情
    if (model === "editSupInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/supplierInvoiceDetail",
        name: "supplierInvoiceDetail",
        query: parameter,
        meta: {
          title: `【编辑】供应商发票详情`
        }
      });
      router.push({ name: "supplierInvoiceDetail", query: parameter });
    }
    // 开票
    if (model === "invoSupInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/supplierInvoiceDetail",
        name: "supplierInvoiceDetail",
        query: parameter,
        meta: {
          title: `【开票】供应商发票开票`
        }
      });
      router.push({ name: "supplierInvoiceDetail", query: parameter });
    }
    // 重新生成
    if (model === "reInvoSupInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/supplierInvoiceDetail",
        name: "supplierInvoiceDetail",
        query: parameter,
        meta: {
          title: `【开票】供应商发票重新生成`
        }
      });
      router.push({ name: "supplierInvoiceDetail", query: parameter });
    }
    // 云采确认
    if (model === "exaimeSupInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/supplierInvoiceDetail",
        name: "supplierInvoiceDetail",
        query: parameter,
        meta: {
          title: `【核对】发票云采确认`
        }
      });
      router.push({ name: "supplierInvoiceDetail", query: parameter });
    }
    // 查看采购合同
    else if (model === "viewProcure") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_procure/components/procureContractDetail",
        name: "procureContractDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}采购合同`
        }
      });
      router.push({ name: "procureContractDetail", query: parameter });
    }
    // 查看供应商结算单
    if (model === "viewSupSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/supplier/components/supplierSettlementDetail",
        name: "supplierSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}结算单`
        }
      });
      router.push({ name: "supplierSettlementDetail", query: parameter });
    }
    // 查看供货订单
    if (model === "viewSupplygoodsOrder") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_SupplyManage/ERP_SupplygoodsOrder/components/SelectSupplygoodsOrder",
        name: "SelectSupplygoodsOrder",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}供货订单`
        }
      });
      router.push({ name: "SelectSupplygoodsOrder", query: parameter });
    }
    // 查看供货清单
    if (model === "viewSupplyOrder") {
      // 保存信息到标签页
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_SupplyManage/ERP_SupplyList/components/SelectSupplyList",
        name: "SelectSupplyList",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}供货清单`
        }
      });
      router.push({ name: "SelectSupplyList", query: parameter });
    }
  }

  // 用于页面刷新，重新获取浏览器地址栏参数并保存到标签页
  const initToDetail = (
    model:
      | "addSupInvoice"
      | "viewSupInvoice"
      | "editSupInvoice"
      | "invoSupInvoice"
      | "reInvoSupInvoice"
      | "exaimeSupInvoice"
      | "viewProcure"
      | "viewSupSettlement"
      | "viewSupplygoodsOrder"
      | "viewSupplyOrder"
  ) => {
    if (getParameter) toDetail(getParameter, model);
  };

  return { toDetail, initToDetail, getParameter, router };
}
